package com.itstudio.portal.service.impl;

import com.itstudio.portal.entity.RestResponse;
import com.itstudio.portal.entity.dto.UserQueryDto;
import com.itstudio.portal.entity.vo.ShowUserForRoleVo;
import com.itstudio.portal.entity.vo.UserDetailInfo;
import com.itstudio.portal.enums.HttpCodeEnum;
import com.itstudio.portal.mapper.AuthInfoMapper;
import com.itstudio.portal.mapper.RoleMapper;
import com.itstudio.portal.mapper.UserDetailMapper;
import com.itstudio.portal.service.UserDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description:
 * @Author: zhaopengbo
 * @Date: 2024/11/23
 */
@Service("UserDetailService")
public class UserDetailServiceImpl implements UserDetailService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private UserDetailMapper userDetailMapper;
    @Override
    public RestResponse<ShowUserForRoleVo> selectUserDetailByCondition(int pageNum, int pageSize, UserQueryDto userQueryDto) {
        //根据分类id查询分类的基本信息
        if(userQueryDto.getRoleId() == null){
            return RestResponse.failure(HttpCodeEnum.ROLE_ID_NOT_EXIST);
        }
        ShowUserForRoleVo showUserForRoleVo = roleMapper.getRoleSimpleById(userQueryDto.getRoleId());
        if(showUserForRoleVo == null){
            return RestResponse.failure(HttpCodeEnum.ROLE_ID_NOT_EXIST);
        }

        Integer total = userDetailMapper.countUserByRole(userQueryDto);
        showUserForRoleVo.setTotal(total);
        showUserForRoleVo.setCurrent(pageNum);

        int offset =(pageNum-1) * pageSize;
        List<UserDetailInfo> userDetailInfos = userDetailMapper.selectUserInfoByCondition(offset,pageSize,userQueryDto);
        showUserForRoleVo.setUserDetailInfo(userDetailInfos);
        showUserForRoleVo.setSize(userDetailInfos.size());
        return RestResponse.success(showUserForRoleVo);
    }

}
